package com.xiaochen.user.controller;

import com.xiaochen.model.dto.h5.UserLoginDto;
import com.xiaochen.model.dto.h5.UserRegisterDto;
import com.xiaochen.model.entity.user.UserInfo;
import com.xiaochen.model.vo.common.Result;
import com.xiaochen.model.vo.common.ResultCodeEnum;
import com.xiaochen.model.vo.h5.UserInfoVo;
import com.xiaochen.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.xiaochen.user.service.impl.UserInfoServiceImpl.USER_LOGIN_TOKEN;

@Tag(name = "会员用户接口")
@RestController
@RequestMapping("api/user/userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 会员注册
     * 默认账号:13886218394
     * 这个账号的默认验证码:6666
     * 默认密码:111111
     * @param userRegisterDto
     * @return
     */
    @Operation(summary = "会员注册")
    @PostMapping("register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto){
        userInfoService.register(userRegisterDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户登录
     */
    @Operation(summary = "会员登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDto userLoginDto){
        String token = userInfoService.login(userLoginDto);
        return Result.build(token, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取用户信息
     */
    @Operation(summary = "获取用户信息")
    @GetMapping("auth/getCurrentUserInfo")
    public Result<UserInfo> getCurrentUserInfo(HttpServletRequest request){
        // 1.从请求头中获取token
        String token = request.getHeader("token");
        // 2.根据token在redis中查询用户信息
        UserInfoVo userInfoVo = userInfoService.getCurrentUserInfo(token);
        System.out.println(USER_LOGIN_TOKEN);
        // 3.返回一个对象(需要什么信息封装一个对象返回)
        return Result.build(userInfoVo, ResultCodeEnum.SUCCESS);
    }
}
